package com.bbs.common.utils;

import com.bbs.common.exception.CommonException;
import org.springframework.web.context.request.RequestAttributes;
import org.springframework.web.context.request.RequestContextHolder;

import java.util.Objects;

/**
 * description
 *
 * @author yangjiajia
 * @createdAt 2024/5/8 17:16
 */
public class RequestContextUtils {

    public static Object get(String key) {
        Object attribute = Objects.requireNonNull(RequestContextHolder.getRequestAttributes()).getAttribute(key, RequestAttributes.SCOPE_REQUEST);
        if (null == attribute) {
            throw new CommonException("未找到属性,key:" + key);
        }
        return attribute;
    }

    public static Integer getUserId() {
        return Integer.parseInt(get("id").toString());
    }

    public static String getAccount() {
        return get("account").toString();
    }
}
